package club.kingon.sql.builder.util;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 * @author dragons
 * @since 2023/7/14
 */
public class PatternUtils {

    private final static Map<String, Pattern> PATTERN_CACHE_MAP = new ConcurrentHashMap<>();

    public static boolean matches(CharSequence source, String regex) {
        Pattern p = PATTERN_CACHE_MAP.get(regex);
        if (Objects.isNull(p)) {
            p = Pattern.compile(regex);
            PATTERN_CACHE_MAP.put(regex, p);
        }
        return p.matcher(source).matches();
    }
}
